import axios from 'axios'
import Qs from 'qs'
import Vue from 'vue'

// 创建axios实例
export const http = axios.create({
  baseURL: process.env.BASE_API, // api的base_url
  method: 'POST',
  timeout: 10000, // 请求超时时间
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  transformRequest: (req) => {
    req = Qs.stringify(req)
    return req
  },
  transformResponse: (res) => {
    if (typeof res === 'string') {
      res = JSON.parse(res)
    }
    return res
  }
})

http.interceptors.response.use((res) => {
  return res.data
}, (error) => {
  return Promise.reject(error)
})

Vue.prototype.$http = http
